package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Author 喵喵喵
 * Date 2025/6/6 10:53
 */
@Api(tags="菜品管理")
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dto) {
        dishService.addDish(dto);
        //清除缓存


        redisTemplate.delete("dish_"+dto.getCategoryId());
        return Result.success();
    }

    @ApiOperation("菜品的分类查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){
        PageResult pageResult=dishService.page(dto);
        return Result.success(pageResult);
    }
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getDishesByCategoryId(Integer categoryId){
        List<DishVO> dishVOList=dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishVOList);
    }

    @ApiOperation("菜品的批量删除")
    @DeleteMapping
    public Result deleteDishByIds(@RequestParam List<Long> ids) {
        dishService.deleteDishByIds(ids);
        //清除所有缓存
        Set key=redisTemplate.keys("dish_*");
        redisTemplate.delete(key);
        return Result.success();

    }

    @ApiOperation("根据id查询菜品详情")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        DishVO dishVO=dishService.getDishById(id);
        return Result.success(dishVO);
    }
    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto) {
        dishService.updateDish(dto);
        //清除缓存
        redisTemplate.delete("dish_"+dto.getCategoryId());
        return Result.success();
    }
    @ApiOperation("启用禁用菜品状态")
    @PostMapping("/status/{status}")
    public Result StartORStopStatus(@PathVariable Integer status, Integer id) {
        dishService.StartORStopStatus(status, id);
        //清除所有缓存
        redisTemplate.delete("dish_*");
        return Result.success();
    }

}
